package controller;

import dao.ArauditorDetailsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.repertory;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/tongji")
public class tongji {
    @Autowired
    public ArauditorDetailsDao arauditorDetailsDao;
    //展示近半年top5的手机名称销售额和本月、上个月的一个差距
    @RequestMapping("xiaoshouTop5")
    @ResponseBody
    public Map<String,Object> xiaoshouTop5(){
      Map<String,Object> map=new HashMap<>();
      //取出近半年销售量top前5的数据
        List<repertory>list=arauditorDetailsDao.showTop5();
        String []name=new String[5];
        List<Object>lists=new ArrayList<>();
        int i=0;
        for(repertory repertory:list){
          /*  List<Integer> list1=new ArrayList<>();*/
            //根据top前5的商品名称取出上个月的销量额
            String shangtotal=arauditorDetailsDao.shang(repertory.getRepertoryName());
            if(shangtotal==null||shangtotal==""){
                shangtotal="0";
            }
            //根据top前5的商品名称取出本月的销量额
            String bentotal=arauditorDetailsDao.ben(repertory.getRepertoryName());
            if(bentotal==null||bentotal==""){
                bentotal="0";
            }
            name[i]=repertory.getRepertoryName();
            lists.add(repertory.getNums());
            lists.add(Integer.parseInt(shangtotal));
            lists.add(Integer.parseInt(bentotal));
            i=i+1;
        }
        map.put("repertoryNames",name);
        map.put("sums",lists);
      return map;
    }
    @RequestMapping("yingye")
    @ResponseBody
    public Map<String,Object> yingye(){
        Map<String,Object> map=new HashMap<>();
        List<Object> xq=new ArrayList<>();
        List<Object> count=new ArrayList<>();
        xq.add("上一年");
        xq.add("今年");
        String one=arauditorDetailsDao.oneyear();//统计上一年的销售
        double k;
        if(one==null){
            k=0.00;
        }else{
            k=Double.parseDouble(one);
        }
        count.add(k);
        String ben=arauditorDetailsDao.benyear();//统计今年的销售
        double p;
        if(ben==null){
            p=0.00;
        }else{
            p=Double.parseDouble(ben);
        }
        count.add(p);
        map.put("xq",xq);
        map.put("count",count);
        return map;
    }
    @RequestMapping("yingyes")
    @ResponseBody
    public Map<String,Object> yingyes(){
        Map<String,Object> map=new HashMap<>();
        List<Object> xq=new ArrayList<>();
        List<Object> count=new ArrayList<>();
        xq.add("上一年");
        xq.add("今年");
        String one=arauditorDetailsDao.oneyears();//统计上一年的销售
        double k;
        if(one==null){
            k=0.00;
        }else{
            k=Double.parseDouble(one);
        }
        count.add(k);
        String ben=arauditorDetailsDao.benyears();//统计今年的销售
        double p;
        if(ben==null){
            p=0.00;
        }else{
            p=Double.parseDouble(ben);
        }
        count.add(p);
        map.put("xq",xq);
        map.put("count",count);
        return map;
    }
    @RequestMapping("times")
    @ResponseBody
    public Map<String,Object> times(){
        Map<String,Object> map=new HashMap<>();
        List<Object> xq=new ArrayList<>();
        List<Object> count=new ArrayList<>();
        for (int i=0;i<7;i++){
            String time=arauditorDetailsDao.times(i);
            String totalNum=arauditorDetailsDao.timesTotal(time);
            int k=0;
            if(totalNum==null){
                k=0;
            }else{
                k=Integer.parseInt(totalNum);
            }
            xq.add(time);
            count.add(k);
        }
        map.put("xq",xq);
        map.put("count",count);
        return map;
    }
}
